package Week.w154;

import org.junit.Test;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

public class Solution01 {

    public int maxNumberOfBalloons(String text) {
        if(text==null){
            return 0;
        }
        int bNum=0;
        int aNum=0;
        int lNum=0;
        int oNum=0;
        int nNum=0;
        for(int i=0;i<text.length();i++){
            switch (text.charAt(i)){
                case  'b':
                   bNum++;
                   break;
                case  'a':
                    aNum++;
                    break;
                case  'l':
                    lNum++;
                    break;
                case  'o':
                    oNum++;
                    break;
                case  'n':
                    nNum++;
                    break;
                default:
                    break;
            }
        }
        lNum=lNum/2;
        oNum=oNum/2;
        return Math.min(bNum,Math.min(aNum,Math.min(lNum,Math.min(oNum,nNum))));
    }
    public String reverseParentheses1(String s) {
        if(s==null||s.length()==0){
            return s;
        }
        char[] chars=s.toCharArray();
        StringBuilder stringBuilder=new StringBuilder();
        int firstIndex=s.indexOf('(');
        int lastIndex=s.lastIndexOf(')');
        boolean flag=true;
        int length=s.length();
        while (firstIndex < lastIndex && firstIndex >= 0&&lastIndex>=0){
            //swap(firstIndex,lastIndex,chars);
            if(true){

                firstIndex=s.indexOf('(',firstIndex+1);
                s=s.substring(0,lastIndex);
                lastIndex=s.lastIndexOf(')');
                firstIndex=length-firstIndex;
                lastIndex=length-lastIndex;
            }else{
                firstIndex=s.indexOf('(',firstIndex+1);
                s=s.substring(0,lastIndex);
                lastIndex=s.lastIndexOf(')');
            }

        }

        for(int i=0;i<chars.length;i++){
            if(chars[i]=='('||chars[i]==')'){
                continue;
            }
            stringBuilder.append(chars[i]);
        }
        return stringBuilder.toString();
    }


    public String reverseParentheses(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        LinkedList<Character> queue = new LinkedList<>();
        Stack<Character> stackAll = new Stack<>();
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ')') {
                while (stackAll.size() > 0) {
                    c = stackAll.pop();
                    if (c == '(') {
                        break;
                    }
                    stack.push(c);
                }

                while (stack.size() > 0) {
                    stackAll.push(stack.pop());
                }
            } else {
                stackAll.push(c);

            }

        }
        while (stackAll.size() != 0) {
            queue.addLast(stackAll.pop());
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (queue.size() != 0) {
            stringBuilder.append(queue.pop());
        }
        return stringBuilder.toString();
    }


    @Test
    public void test(){
        String test1="(u(love)i)";
        String test="f(ul)ao(t(y)qbn)()sj";
        String test3="a(bcdefghijkl(mno)p)q";

       // System.out.println(reverseParentheses(test1));
        System.out.println(reverseParentheses(test1));
      //  System.out.println(reverseParentheses(test3));

    }
}
